//购物车
define('cartServers', ['angular', 'jquery', 'underscore'], function (angular, $, _) {
    var module = angular.module('servers');
    module.factory('cart', ['$http', function ($http) {
        return {
            goods: [],
            add: function (g) {
                var i = _.indexOf(this.goods, g);
                if (i == -1) {
                    //购物车里没有商品
                    g.num = 0;
                    this.goods.push(g);
                } else {
                    this.goods[i].num++;
                }
            },
            //获取总价
            getTotalPrice: function () {
                var total = 0;
                for ($i = 0; $i < this.goods.length; $i++) {
                    total += (this.goods[$i].price * this.goods[$i].num);
                }
                return total;
            }
        };
    }])
});